/**
 * Created by zhaojm on 15/3/14.
 */
game.Hellolayer = cc.Layer.extend({

    //heartlist : null,



    ctor:function(){
        this._super();
        var background = new cc.Sprite(res.hello_jpg);

        background.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height / 2));

        this.addChild(background);


        //this.blue_fish = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('fish_blue_0.png'));
        //this.addChild(this.blue_fish);
        //this.blue_fish.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.5));
        var fishframe = cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.a.idle[0]);
        var fish1 = new cc.MenuItemImage(fishframe, fishframe, fishframe, this.onMenuCallback1, this);
        fish1.setPosition(cc.p(cc.winSize.width * 0.3, cc.winSize.height * 0.45));
        //this.addChild(item_fish_blue);

        var fishframe1 = cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.b.idle[0]);
        var fish2 = new cc.MenuItemImage(fishframe1, fishframe1, fishframe1, this.onMenuCallback2, this);
        fish2.setPosition(cc.p(cc.winSize.width * 0.7, cc.winSize.height * 0.45));


        //var fishframe2 = cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.c.idle[0]);
        //var fish3 = new cc.MenuItemImage(fishframe2, fishframe2, fishframe2, this.onMenuCallback3, this);
        ////fish3.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.5));


        this.fishItemList = [fish1, fish2];



        var menu = new cc.Menu(fish1, fish2);
        //menu.alignItemsVertically();
        //menu.alignItemsHorizontally();
        this.addChild(menu);
        //menu.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.4));
        menu.setPosition(cc.p(0, 0));

        //this.fishname = cc.LabelTTF.create('小鱼', 'Arial', 24);
        //this.fishname.setColor(cc.color(219, 112, 147));
        //
        ////this.fishname = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('fishname0'));
        //
        //this.fishname.setPosition(cc.p(cc.winSize.width * 0.4, cc.winSize.height * 0.2));
        //this.fishname.setAnchorPoint(cc.p(1, 0));
        //this.addChild(this.fishname);

        //this.heartlist = [];
        //for(var i = 0; i < 5; i++){
        //    var heart = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('heart.png'));
        //    heart.setAnchorPoint(cc.p(0, 0));
        //    if(this.heartlist.length == 0){
        //        heart.setPosition(cc.p(this.fishname.getPositionX() + 10, this.fishname.getPositionY()));
        //    }else {
        //        var lastheart = this.heartlist[this.heartlist.length - 1];
        //        heart.setPosition(cc.p(lastheart.getPositionX() + lastheart.getContentSize().width, lastheart.getPositionY()));
        //    }
        //
        //    this.heartlist.push(heart);
        //    this.addChild(heart);
        //
        //}
        this._fishHeart = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('fish_heart_0.png'));
        this.addChild(this._fishHeart);
        this._fishHeart.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.3));


        this.refreshUI(game.Config.fishType.a);


        //
        var startframe = cc.spriteFrameCache.getSpriteFrame("start.png");
        this._startItem = new cc.MenuItemImage(startframe,startframe, startframe, this.onMenuStart, this);
        this._startItem.setPosition(cc.p(cc.winSize.width * 0.5, cc.winSize.height * 0.1));


        var moreframe = cc.spriteFrameCache.getSpriteFrame("home.png");
        var moreitem = new cc.MenuItemImage(moreframe,moreframe, moreframe, this.onMore, this);
        moreitem.setAnchorPoint(cc.p(1, 1));
        moreitem.setPosition(cc.p(cc.winSize.width * 0.95, cc.winSize.height * 0.95));
        //moreitem.setScale(0.5);

        var menu2 = new cc.Menu(this._startItem, moreitem);
        //menu.alignItemsVertically();
        //menu2.alignItemsHorizontally();
        this.addChild(menu2);
        menu2.setPosition(cc.p(0, 0));





    },
    onMore:function(){
        window.location.href="http://mingz.me";
    },
    onMenuStart:function(){
        var scene = new game.MainScene();
        //scene.setFishType(game.Config.fishType.a)
        cc.director.runScene(scene);
    },

    onMenuCallback1:function(){
        //var scene = new game.MainScene();
        //scene.setFishType(game.Config.fishType.a)
        //cc.director.runScene(scene);

        this.refreshUI(game.Config.fishType.a);
    },


    onMenuCallback2:function(sender){
        //cc.log('onMenuCallback2');

        //var scene = new game.MainScene();
        //scene.setFishType(game.Config.fishType.b)
        //cc.director.runScene(scene);

        this.refreshUI(game.Config.fishType.b);
    },

    //onMenuCallback3:function (sender) {
    //    //cc.log('onMenuCallback');
    //    //var scene = new game.MainScene();
    //    //scene.setFishType(game.Config.fishType.c)
    //    //cc.director.runScene(scene);
    //
    //    this.refreshUI(game.Config.fishType.c);
    //},

    refreshUI : function(type){
        game.Config.fishRes = type;

        //for(var i = 0; i < this.heartlist.length; i++){
        //    if(i < game.Config.fishRes.heart){
        //
        //        this.heartlist[i].setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('heart.png'));
        //    }else{
        //
        //        this.heartlist[i].setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('heart_2.png'));
        //    }
        //}

        //this.fishname.setString(game.Config.fishRes.fishname);
        //this.fishname.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame(game.Config.fishRes.fishname));

        if(type == game.Config.fishType.a){

            this.setMenuItemImg(this.fishItemList[0], cc.spriteFrameCache.getSpriteFrame('fishlight0.png'));
            this.setMenuItemImg(this.fishItemList[1], cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.b.idle[0]));
            //this.setMenuItemImg(this.fishItemList[2], cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.c.idle[0]));
            this._fishHeart.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('fish_heart_0.png'));
        }else if(type == game.Config.fishType.b){

            this.setMenuItemImg(this.fishItemList[0], cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.a.idle[0]));
            this.setMenuItemImg(this.fishItemList[1], cc.spriteFrameCache.getSpriteFrame('fishlight1.png'));
            //this.setMenuItemImg(this.fishItemList[2], cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.c.idle[0]));
            this._fishHeart.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('fish_heart_1.png'));
        }
        // else if(type == game.Config.fishType.c){
        //
        //    this.setMenuItemImg(this.fishItemList[0], cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.a.idle[0]));
        //    this.setMenuItemImg(this.fishItemList[1], cc.spriteFrameCache.getSpriteFrame(game.Config.fishType.b.idle[0]));
        //    this.setMenuItemImg(this.fishItemList[2], cc.spriteFrameCache.getSpriteFrame('fishlight2.png'));
        //}


    },

    setMenuItemImg : function(item, frame){

        item.setNormalSpriteFrame(frame);
        item.setDisabledSpriteFrame(frame);
        item.setSelectedSpriteFrame(frame);
    },

    startAction:function(){
        cc.log('start action ...');
        //this._startItem.runAction(cc.Shaky3D.create(10, cc.size(20, 20), 10, false));
    },

});

game.HelloScene = cc.Scene.extend({
    onEnter:function(){
        this._super();
        var layer = new game.Hellolayer();
        this.addChild(layer);
        layer.startAction();
    },
});